﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace FileWatcher.Controls
{
    public delegate void GetSettingsDelegate(Guid identifier);
    public delegate void FolderChangeDelegate(Guid identifier, string folder);
    public delegate void SaveSettingsDelegate(Guid identifier, FileWatcherSettings settings);
    public delegate void UpdateWatchDogStateDelegate(Guid identifier, FileWatcherSettings settings);
    /// <summary>
    /// Interaction logic for FileWatcherSettings.xaml
    /// </summary>
    public partial class FileWatcherControl : TabItem
    {
        public event GetSettingsDelegate GetSettingsEvent;
        public event FolderChangeDelegate FolderChangeEvent;
        public event SaveSettingsDelegate SaveSettingsEvent;
        public event UpdateWatchDogStateDelegate UpdateWatchDogStateEvent;

        private bool m_loading;
        private FileWatcherSettings m_settings;

        public int Selected
        {
            set 
            {
                this.m_loading = true;
                this.m_folderListBox.SelectedIndex = value;
                this.m_loading = false;
            }
        }

        public FileWatcherControl()
        {
            InitializeComponent();
        }

        private void m_addButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog browser = new System.Windows.Forms.FolderBrowserDialog();
            browser.RootFolder = System.Environment.SpecialFolder.MyComputer;
            browser.ShowNewFolderButton = true;

            if (browser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ListBoxItem item = new ListBoxItem();
                item.Tag = Guid.NewGuid();
                item.Content = browser.SelectedPath;

                this.m_folderListBox.Items.Add(item);

                this.FolderChangeEvent((Guid)item.Tag, (string)item.Content);
            }
        }

        private void m_removeButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.m_folderListBox.SelectedIndex == -1) return;

            ListBoxItem item = (ListBoxItem)this.m_folderListBox.Items[this.m_folderListBox.SelectedIndex];

            if (this.m_folderListBox.SelectedIndex == this.m_folderListBox.Items.Count - 1)
            {
                this.m_folderListBox.SelectedIndex -= 1;
            }
            else
            {
                this.m_folderListBox.SelectedIndex += 1;
            }

            this.m_folderListBox.Items.Remove(item);

            this.FolderChangeEvent((Guid)item.Tag, (string)item.Content);
        }

        public void AddFolder(Guid identifier, string folder)
        {
            ListBoxItem item = new ListBoxItem();
            item.Tag = identifier;
            item.Content = folder;

            this.m_folderListBox.Items.Add(item);
        }

        public void UpdateSettings(FileWatcherSettings settings)
        {
            this.m_loading = true;

            this.m_settings = settings;

            this.m_acceptTextBox.Text = settings.AcceptPattern;
            this.m_actionComboBox.SelectedItem = "iTunes";
            this.m_cycleTextBox.Text = settings.Cycle.ToString();
            this.m_deleteCheckBox.IsChecked = settings.Delete;
            this.m_processExistCheckBox.IsChecked = settings.ProcessExisting;
            this.m_recursiveCheckBox.IsChecked = settings.Recursive;
            this.m_timeoutTextBox.Text = settings.Timeout.ToString();

            if (settings.Action == null)
            {
                this.m_actionComboBox.SelectedIndex = 0;
            }
            else
            {
                for (int i = 0; i < this.m_actionComboBox.Items.Count; i++)
                {
                    if (settings.Action == ((ComboBoxItem)this.m_actionComboBox.Items[i]).Content as string)
                    {
                        this.m_actionComboBox.SelectedIndex = i;
                    }
                }
            }

            switch (settings.State)
            {
                case FileWatcherSettings.FileWatcherState.Running:
                    {
                        this.m_startButton.IsEnabled = false;
                        this.m_stopButton.IsEnabled = true;
                    }
                    break;
                case FileWatcherSettings.FileWatcherState.Stopped:
                    {
                        this.m_startButton.IsEnabled = true;
                        this.m_stopButton.IsEnabled = false;
                    }
                    break;
            }

            this.m_loading = false;
        }

        private void m_folderListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.m_folderListBox.SelectedIndex != -1)
            {
                ListBoxItem item = (ListBoxItem)this.m_folderListBox.Items[this.m_folderListBox.SelectedIndex];

                this.GetSettingsEvent((Guid)item.Tag);
            }
        }

        private void m_recursiveCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (this.m_loading) return;

            this.m_settings.Recursive = true;
            this.m_saveButton.IsEnabled = true;
        }

        private void m_recursiveCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if (this.m_loading) return;

            this.m_settings.Recursive = false;
            this.m_saveButton.IsEnabled = true;
        }

        private void m_processExistCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (this.m_loading) return;

            this.m_settings.ProcessExisting = true;
            this.m_saveButton.IsEnabled = true;
        }

        private void m_processExistCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if (this.m_loading) return;

            this.m_settings.ProcessExisting = false;
            this.m_saveButton.IsEnabled = true;
        }

        private void m_deleteCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (this.m_loading) return;

            this.m_settings.Delete = true;
            this.m_saveButton.IsEnabled = true;
        }

        private void m_deleteCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if (this.m_loading) return;

            this.m_settings.Delete = false;
            this.m_saveButton.IsEnabled = true;
        }

        private void m_actionComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.m_loading) return;

            this.m_saveButton.IsEnabled = true;
        }

        private void m_saveButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.m_stopButton.IsEnabled)
            {
                System.Windows.Forms.MessageBox.Show("Changes won't be applied until FileWatcher is restarted", "Restart required", System.Windows.Forms.MessageBoxButtons.OK);
            }

            this.m_settings.Action = this.m_actionComboBox.SelectionBoxItem as string;
            this.m_settings.AcceptPattern = this.m_acceptTextBox.Text;
            this.m_settings.Cycle = int.Parse(this.m_cycleTextBox.Text);
            this.m_settings.Timeout = int.Parse(this.m_timeoutTextBox.Text);

            ListBoxItem item = (ListBoxItem)this.m_folderListBox.Items[this.m_folderListBox.SelectedIndex];
            this.SaveSettingsEvent((Guid)item.Tag, this.m_settings);

            this.m_saveButton.IsEnabled = false;
        }

        private void m_startButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.m_saveButton.IsEnabled)
            {
                if (System.Windows.Forms.MessageBox.Show("There are unsaved changes, these will not be applied until the filewatcher is restarted", "Unsaved changes", System.Windows.Forms.MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel)
                {
                    return;
                }
            }

            this.m_startButton.IsEnabled = false;
            this.m_stopButton.IsEnabled = true;

            ListBoxItem item = (ListBoxItem)this.m_folderListBox.Items[this.m_folderListBox.SelectedIndex];
            this.m_settings.State = FileWatcherSettings.FileWatcherState.Running;
            this.UpdateWatchDogStateEvent((Guid)item.Tag, this.m_settings);
        }

        private void m_stopButton_Click(object sender, RoutedEventArgs e)
        {
            this.m_startButton.IsEnabled = true;
            this.m_stopButton.IsEnabled = false;

            ListBoxItem item = (ListBoxItem)this.m_folderListBox.Items[this.m_folderListBox.SelectedIndex];
            this.m_settings.State = FileWatcherSettings.FileWatcherState.Stopped;
            this.UpdateWatchDogStateEvent((Guid)item.Tag, this.m_settings);
        }

        private void m_timeoutTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.m_loading) return;

            this.m_saveButton.IsEnabled = true;
        }

        private void m_cycleTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.m_loading) return;

            this.m_saveButton.IsEnabled = true;
        }

        private void m_acceptTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.m_loading) return;

            this.m_saveButton.IsEnabled = true;
        }
    }
}
